﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;

namespace Fly.FastEval
{
    /// <summary>
    /// FastEval 扩展
    /// </summary>
    public static class FastEvalExtensions
    {
        private static readonly DynamicPropertyAccessorCache SCache = new DynamicPropertyAccessorCache();
        /// <summary>
        /// 获取指定空间的指定属性的值
        /// </summary>
        /// <param name="control"></param>
        /// <param name="o"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static object FastEval(this Control control, object o, string propertyName)
        {
            return SCache.GetAccessor(o.GetType(), propertyName).GetValue(o);
        }

        public static object FastEval(this TemplateControl control, string propertyName)
        {
            return control.FastEval(control.Page.GetDataItem(), propertyName);
        }
        /// <summary>
        /// 获取指定对象指定属性的值
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象</param>
        /// <param name="propertyName">属性名称</param>
        /// <returns></returns>
        public static object FastEval<T>(this T obj, string propertyName)
        {
            return SCache.GetAccessor(typeof (T), propertyName).GetValue(obj);
        }

        public static void FastEval<T>(this T obj, string propertyName, object value)
        {
            SCache.GetAccessor(typeof(T), propertyName).SetValue(obj,value);
        }
    }
}
